package common.apis;

import common.globol.Config;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import utils.HttpUtil;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 神话验证码平台API
 */
public class ShenHuaApi {

    /**
     * token信息.
     */
    private String token=Config.getConfig().getShenhua_token();
    /**
     * 当前电话号码
     */
    private String phone="";
    /**
     * 当前验证码
     */
    private String code="";
    /**
     * 当前项目ID
     */
    private String item="";


    /**
     * Login.
     *
     * @throws IOException        the io exception
     * @throws URISyntaxException the uri syntax exception
     */
    public void login() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/uLogin";
        Map<String,String> param=new HashMap<>();
        param.put("uName","254992364");
        param.put("pWord","123456");
        param.put("Developer","n%2fndT29jxLQEEBHBLT4yDg%3d%3d");
        token=getResponseString(HttpUtil.getInstance().doGet(url,param)).split("&")[0];
        System.out.println("请修改shenhua.properties文件，获取到新的token："+token);
    }

    /**
     * Gets phone.
     *
     * @throws IOException        the io exception
     * @throws URISyntaxException the uri syntax exception
     */
    public void getPhone() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/GetPhone";
        Map<String,String> param=new HashMap<>();
        param.put("ItemId",item);
        param.put("token",token);
        param.put("Count","1");
        //param.put("Area","%E4%B8%8D%E9%99%90");
        param.put("PhoneType","0");
        param.put("Phone","");
        param.put("onlyKey","");
        phone=getResponseString(HttpUtil.getInstance().doGet(url,param)).substring(0,11);
        //System.out.println("获取到手机号："+phone);
    }

    /**
     * Gets message.
     *
     * @apiNote
     *
     * if(codetemp.indexOf("xxxx")!=-1){   //需要自己定义短信内容关键字判断是否成功获取验证码
     * code=codetemp.substring(31,37);  //需要自己定义验证码截取索引
     * break;
     * }
     *
     * @return 0代表成功 ，1代表1分钟也没有获取到验证码
     * @throws IOException          the io exception
     * @throws URISyntaxException   the uri syntax exception
     * @throws InterruptedException the interrupted exception
     */
    public int getMessage() throws IOException, URISyntaxException, InterruptedException {
        code="";
        String url="http://api.shjmpt.com:9002/pubApi/GetMessage";
        Map<String,String> param=new HashMap<>();
        param.put("token",token);
        param.put("PhoneList",phone+"-"+item+";");
        String codetemp="";
        for(int i=0;i<10;i++){
            codetemp=getResponseString(HttpUtil.getInstance().doGet(url,param));
            if(codetemp.indexOf("验证码")!=-1){
                code=codetemp.substring(31,37);
                break;
            }
            Thread.sleep(6100);
        }
        if(code.length()>0){
            return 0;
        }else{
            return 1;
        }
    }

    /**
     * Release phone.
     *
     * @apiNote
     * 释放手机号，验证码获取完后需要释放手机号
     *
     * @throws IOException        the io exception
     * @throws URISyntaxException the uri syntax exception
     */
    public void releasePhone() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/ReleasePhone";
        Map<String,String> param=new HashMap<>();
        param.put("token",token);
        param.put("phoneList",phone+"-"+item+";");
        getResponseString(HttpUtil.getInstance().doGet(url,param));
    }

    /**
     * Exit.
     *
     * @apiNote
     * 退出登录，toekn会失效；
     *
     * @throws IOException        the io exception
     * @throws URISyntaxException the uri syntax exception
     */
    public void exit() throws IOException, URISyntaxException {
        String url="http://api.shjmpt.com:9002/pubApi/uExit";
        Map<String,String> param=new HashMap<>();
        param.put("token",token);
        getResponseString(HttpUtil.getInstance().doGet(url,param));
        token=null;
    }

    /**
     * Gets response string.
     *
     * @param response the response
     * @return the response string
     * @throws IOException the io exception
     */
    public String getResponseString(HttpResponse response) throws IOException {
        return  EntityUtils.toString(response.getEntity());
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }


}
